<template>
  <BaseLayout title="角色管理" @query="init" @reset="reset">
    <template #query>
      <el-form :inline="true" :model="queryForm">
        <el-form-item label="角色名称">
          <el-input clearable v-model="queryForm.roleName" placeholder="请输入角色名称"/>
        </el-form-item>

        <el-form-item label="角色编号">
          <el-input clearable v-model="queryForm.roleCode" placeholder="请输入角色编号"/>
        </el-form-item>

      </el-form>
    </template>
    <template #button>
      <el-button type="primary" @click="addRole">新增</el-button>
    </template>
    <template #table>
      <el-table v-loading="loading" :data="data" height="100%" class="w-full">
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" align="center" header-align="center"></el-table-column>
        <el-table-column prop="roleCode" label="角色编号" align="center" header-align="center"></el-table-column>
        <el-table-column header-align="center" align="center" prop="enabled" label="是否启用" width="80">
          <template #default="scope">
            <BaseTag :value="scope.row.enabled" :dict="enableOptions"></BaseTag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" align="center" header-align="center">
          <template #default="scope">
            <el-button link type="primary" @click="editRole(scope.row.roleId)">修改</el-button>
            <el-button link type="primary" @click="setAuth(scope.row.roleId)">设置权限</el-button>
            <el-button link type="danger" @click="delRole(scope.row.roleId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddOrUpdateRole @load="init" ref="addOrUpdateRoleRef"></AddOrUpdateRole>
      <SetRoleAuth ref="setRoleAuthRef"></SetRoleAuth>
    </template>
    <template #page>
      <BasePage @load="init" v-model:current="queryForm.current" v-model:size="queryForm.size"
                :total="queryForm.total"></BasePage>
    </template>
  </BaseLayout>
</template>

<script setup lang="ts" name="role">
import AddOrUpdateRole from './AddOrUpdateRole.vue'
import {nextTick, onMounted, reactive, ref} from "vue";
import {deleteRoleByRoleId, listRolePage, selectRoleByRoleId} from "/@/api/system/RoleApi";
import {messageBox} from "/@/utils/BjxUtils";
import {DictDataModel} from "/@/api/system/model/DictModel";
import {RoleQueryModel, RoleModel} from "/@/api/system/model/RoleModel";
import SetRoleAuth from './SetRoleAuth.vue'
import {useDictStore} from "/@/store/modules/dict";

const enableOptions = ref<DictDataModel[]>([])
const data = ref<RoleModel[]>([])
const addOrUpdateRoleRef = ref()
const setRoleAuthRef = ref()
const loading = ref(false)
const queryForm = reactive<RoleQueryModel>({
  roleName: '',
  roleCode: '',
  size: 10,
  current: 1,
  total: 0
})
const init = async () => {
  loading.value = true
  let res = await listRolePage({...queryForm}, () => {
    loading.value = false
  })
  data.value = res.records
  queryForm.size = res.size
  queryForm.current = res.current
  queryForm.total = res.total
}
/**
 * 新增角色
 */
const addRole = () => {
  addOrUpdateRoleRef.value.init("新增角色")
}
/**
 * 修改角色
 */
const editRole = async (roleId: string) => {
  loading.value = true
  let res = await selectRoleByRoleId(roleId)
  addOrUpdateRoleRef.value.init("修改角色", res)
  loading.value = false
}
/**
 * 删除角色
 */
const delRole = async (roleId: string) => {
  messageBox("您确定要删除这个角色吗?", async () => {
    loading.value = true
    await deleteRoleByRoleId(roleId, () => {
      loading.value = false
    })
    init()
  })

}
/**
 * 设置权限
 */
const setAuth = (roleId: string) => {
  setRoleAuthRef.value.init(roleId)
}
/**
 * 重置
 */
const reset = () => {
  queryForm.roleName = '',
      queryForm.roleCode = '',
      queryForm.current = 1
  init()
}
onMounted(async () => {
  let dictStore = useDictStore();
  enableOptions.value = await dictStore.getDict('enable_dict')
  nextTick(() => {
    init()
  })

})
</script>

<style scoped>

</style>